package com.wuyoucar.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TCarinfo;
import com.wuyoucar.entity.TCarinfoSource;
import com.wuyoucar.entity.TOrder;
import com.wuyoucar.entity.TUserinfo;
import com.wuyoucar.service.ICarinfoSourceService;
import com.wuyoucar.service.ITCarinfoService;
import com.wuyoucar.service.ITOrderService;
import com.wuyoucar.util.IdcardUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 这是自动核保模块
 * 主要逻辑为:根据用户id去查提交的保单得到vin(车架号)和carnumber(车牌号),
 * 再根据车架号和车牌号去查找车管所和用户绑定的车辆信息，当着两个表都不为空时，
 * 就自动核保成功，否则失败
 */
@RestController
@RequestMapping("/carinfoSource")
public class ICarinfoSourceController {
    //车管所的业务接口
    @Autowired
    private ICarinfoSourceService iCarinfoSourceService;
    //车辆信息的业务接口
    @Autowired
    private ITCarinfoService itCarinfoService;
    //保存保单的业务接口
    @Autowired
    private ITOrderService itOrderService;
    //验证自动保单方法
   @RequestMapping("/autoOrderCheck/{ordernumber}/{insuredpersonname}/{insuredpersonidnumber}")
    public ResultBean autoOrderCheck(@PathVariable("ordernumber") String ordernumber,
                                     @PathVariable("insuredpersonname") String insuredpersonname,
                                     @PathVariable("insuredpersonidnumber") String insuredpersonidnumber){
      // boolean b = IdcardUtil.checkIdCard(insuredpersonidnumber,insuredpersonname);
       System.out.println("insuredpersonname"+insuredpersonname+"insuredpersonidnumber"+insuredpersonidnumber);


       //根据前台提交的用户id查找保单信息
       TOrder order = itOrderService.findOrderByUserId(ordernumber);
       String msg=null;
       System.out.println("order========"+order);
       if (order==null){
           return new ResultBean(400,"自动保单失败");
       }else {
           Map<String, String> map = new HashMap<>();
           map.put("vin", order.getVin());
           map.put("carnumber", order.getCarnumber());
           //根据前台提供车架号和车牌号去车管所查找是否存在这辆车
           TCarinfoSource carinfoSource = iCarinfoSourceService.findCarByCarNumber(map);
           //根据前台提供的车牌号和车架号查找用户绑定的车辆信息是否存在这辆车
           TCarinfo carinfo = itCarinfoService.findCarinfoByVin(map);
           //如果车管所有这辆车和用户绑定的车辆信息表有这辆车则，自动核保成功
           if (carinfoSource != null && carinfo != null&&order.getUserinfo().getPaytimes()<=8) {
               msg = "审核通过";
               map.put("checkstate", "审核通过");
               map.put("remark","审核通过,请点击付款!");
               map.put("ordernumber", order.getOrdernumber());
               itOrderService.modifyCheckState(map);
               return new ResultBean(200, msg, "success");
           } else {
               map.put("checkstate", "自动审核未通过");
               map.put("remark","因订单信息有误,自动审核未通过，请查看车辆信息和身份证号是否有误!");
               map.put("ordernumber", order.getOrdernumber());
               itOrderService.modifyCheckState(map);
               msg = "审核未通过";
               return new ResultBean(400, msg, "fail");
           }
       }
   }
}
